﻿using System;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using System.Collections.Generic;

namespace NanoERP.BaseInfo
{
    [Module("FrmAccSubjectList")]
    public partial class FrmAccSubjectList : ModuleForm
    {
        readonly AccountsubjectBLL _accountsubjectBLL;
        public FrmAccSubjectList()
        {
            InitializeComponent();

            _accountsubjectBLL = new AccountsubjectBLL();

            QueryInfo();

            treeList.AfterExpand += (sender, e) => treeListColumn1.BestFit();
        }

        void QueryInfo()
        {
            treeList.Nodes.Clear();
            treeList.BeginUnboundLoad();
            var list = _accountsubjectBLL.Query(0);
            list.ForEach(r =>
            {
                var tgn = treeList.AppendNode(new object[] { r.Subjectno, r.Subjectname, r.Subjectpym, r.Begininmoney, r.Beginoutmoney, r.Beginmoney, r.Inmoney, r.Outmoney, r.Endmoney, r.Isstandard, r.Note }, null);
                tgn.Tag = r;
                var listSec = _accountsubjectBLL.Query(r.Id);
                RecursiveLoopNodes(listSec, tgn);
            });

            treeList.EndUnboundLoad();
        }

        /// <summary>
        /// 递归循环调用
        /// </summary>
        /// <param name="firList">一级树集合</param>
        /// <param name="pNode">上级节点</param>
        void RecursiveLoopNodes(List<Accountsubject> firList, TreeListNode pNode)
        {
            firList.ForEach(r =>
            {
                var thiNode = treeList.AppendNode(new object[] { r.Subjectno, r.Subjectname, r.Subjectpym, r.Begininmoney, r.Beginoutmoney, r.Beginmoney, r.Inmoney, r.Outmoney, r.Endmoney, r.Isstandard, r.Note }, pNode);
                thiNode.Tag = r;
                var listSec = _accountsubjectBLL.Query(r.Id);
                if (listSec.Count > 0)
                    RecursiveLoopNodes(listSec, thiNode);
            });
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="selectedNode"></param>
        void RefreshTree(TreeListNode selectedNode)
        {
            selectedNode.Nodes.Clear();
            var obj = selectedNode.Tag as Accountsubject;

            if (obj == null)
            {
                QueryInfo();
                return;
            }
            RecursiveLoopNodes(_accountsubjectBLL.Query(obj.Id), selectedNode);
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            var node = treeList.FocusedNode;
            if (node.Level == 0)
            {
                MessageBox.Show(@"不允许增加一级科目的同级科目！", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            var curr = node.Tag as Accountsubject;
            var obj = new Accountsubject { Parentid = curr.Parentid };
            var f = new FrmAccSubjectEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
                RefreshTree(node);
        }

        private void tsbChild_Click(object sender, EventArgs e)
        {
            var node = treeList.FocusedNode;
            if (node.Level == 0)
            {
                MessageBox.Show(@"不允许增加一级科目的子科目！", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            var curr = node.Tag as Accountsubject;
            var obj = new Accountsubject { Parentid = curr.Id };
            var f = new FrmAccSubjectEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
                RefreshTree(node);
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            var node = treeList.FocusedNode;
            if (node.Level == 0)
            {
                MessageBox.Show(@"一级科目不允许修改！", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (node.Level == 1)
            {
                MessageBox.Show(@"系统内置的会计标准科目不允许修改！", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            var obj = node.Tag as Accountsubject;
            var f = new FrmAccSubjectEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
            {
                RefreshTree(node.ParentNode);
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            var node = treeList.FocusedNode;
            if (node.Level <= 1)
            {
                MessageBox.Show(@"当前科目为会计制度的标准科目，不允许删除！", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            var v = (Accountsubject)node.Tag;
            string text;
            if (node.Nodes.Count > 0)
            {
                text = string.Format("请先“删除”该科目“{0}”下级科目.", v.Subjectname);
                MessageBox.Show(text, @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            text = string.Format(@"您要确定要“删除”当前科目“{0}”吗？", v.Subjectname);
            if (MessageBox.Show(text, @"请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _accountsubjectBLL.Delete(v.Id);
                RefreshTree(node.ParentNode);
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
